##################################################################################################################
# Code for Results in Appendix Section A5.1
##################################################################################################################
write.table(data.frame(Description=c("","","","Appendix Section A5: Descriptive Statistics")),row.names=F,col.names=F,quote=F)

##################################################################################################################
# To run, please set the working directory to match the location of the folder containing all replication files.
##################################################################################################################
#setwd("Replication_PSRM_ST24")

#if not yet created, create "output" folder
if("output"%in%list.files()==F){dir.create("output")}

#Clear environment
rm(list=ls());gc();gc();gc();gc()

#If not yet installed, install package
if(length(find.package("dplyr",quiet=T))==0){install.packages("dplyr")}

#Load package
library(dplyr)

#Set POSIX locale
Sys.setlocale(locale="C")

#Load main data set
loball5ciA<-readRDS("Data/PSRM_main_dataset.rds")

###############################################
# Descriptive statistics
###############################################
#number of observations


write.table(data.frame(Description=c("","Results:")),row.names=F,col.names=F,quote=F)
write.table(data.frame(Description=c("","Number of Lobbyist-Client Relationships (Contract and In-House):")),row.names=F,col.names=F,quote=F)
print(nrow(loball5ciA[loball5ciA$type%in%c("contract","inhouse"),])) #43817

write.table(data.frame(Description=c("","Number of Contract Lobbyist-Client Relationships:")),row.names=F,col.names=F,quote=F)
print(nrow(loball5ciA[loball5ciA$type%in%c("contract"),])) #28541

write.table(data.frame(Description=c("","Number of In-House Lobbyist-Client Relationships:")),row.names=F,col.names=F,quote=F)
print(nrow(loball5ciA[loball5ciA$type%in%c("inhouse"),])) #15276

write.table(data.frame(Description=c("","Number of Observations From Non-Licensed Lobbying:")),row.names=F,col.names=F,quote=F)
print(nrow(loball5ciA[loball5ciA$type%in%c("nonlobbyist"),])) #1956

write.table(data.frame(Description=c("","Total Number of Observations:")),row.names=F,col.names=F,quote=F)
print(nrow(loball5ciA)) #45773

descriptives1_mean_obs<-loball5ciA  %>% 
  group_by(halfyear) %>%
  summarise(n=sum(count),time=sum(hours,na.rm=T),money=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T))

write.table(data.frame(Description=c("","Mean Number of Observations, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(mean(descriptives1_mean_obs$n))) #1635
write.table(data.frame(Description=c("","Standard Deviation for Number of Observations, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(sd(descriptives1_mean_obs$n))) #99

write.table(data.frame(Description=c("","Mean Number of Hours Worked, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(mean(descriptives1_mean_obs$time,na.rm=T))) #107844
write.table(data.frame(Description=c("","Standard Deviation for Number of Hours Worked, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(sd(descriptives1_mean_obs$time,na.rm=T))) #28697

write.table(data.frame(Description=c("","Mean Lobbying Expenditures, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(mean(descriptives1_mean_obs$money,na.rm=T))) #14683717
write.table(data.frame(Description=c("","Standard Deviation for Lobbying Expenditures, Across 28 Half-Yearly Periods:")),row.names=F,col.names=F,quote=F)
print(round(sd(descriptives1_mean_obs$money,na.rm=T))) #2314366

agg_time<-loball5ciA %>%
  group_by(halfyear) %>%
  summarise(amount=sum(Rcomparable_total_amounts_cleaned_exp,na.rm=T),time=sum(hours,na.rm=T))

write.table(data.frame(Description=c("","Figure A1: Reported Hours Worked and Lobbying Expenditures, Aggregated by Half-Year")),row.names=F,col.names=F,quote=F)

png("output/PSRM_figA1.png",width = 10,height = 10,units="in",res=300)
  par(mfrow=c(2,1),mar=c(4,6,3,1))
  plot(xaxt="n",cex.axis=1.5,cex.lab=1.5,cex.main=2,main="(1) Hours Worked", as.Date(agg_time$halfyear),agg_time$time,type="l",xlab="Half-Years",ylab="Hours Worked by Lobbyists")
  axis(side=1,cex.axis=1.5, at=c(as.Date("2005-01-01"),as.Date("2007-01-01"),as.Date("2009-01-01"),as.Date("2011-01-01"),as.Date("2013-01-01"),as.Date("2015-01-01")
                                 ,as.Date("2017-01-01")), labels=seq(2005,2017,2))
  
  plot(xaxt="n",cex.axis=1.5,cex.lab=1.5,cex.main=2,main="(2) Expenditures",as.Date(agg_time$halfyear),agg_time$amount,type="l",xlab="Half-Years",ylab="Lobbying Expenditures /\nPayments to Lobbyists")
  axis(side=1,cex.axis=1.5, at=c(as.Date("2005-01-01"),as.Date("2007-01-01"),as.Date("2009-01-01"),as.Date("2011-01-01"),as.Date("2013-01-01"),as.Date("2015-01-01")
                                 ,as.Date("2017-01-01")), labels=seq(2005,2017,2))
dev.off()

write.table(data.frame(Description=c("(N.B. The file for this figure is saved as 'PSRM_figA1.png' in the 'output' folder.)")),row.names=F,col.names=F,quote=F)

#Include code again so that Figure A1 also shown in R if code file is run using source()
par(mfrow=c(2,1),mar=c(4,6,3,1))
plot(xaxt="n",cex.axis=1.5,cex.lab=1.5,cex.main=2,main="(1) Hours Worked", as.Date(agg_time$halfyear),agg_time$time,type="l",xlab="Half-Years",ylab="Hours Worked by Lobbyists")
axis(side=1,cex.axis=1.5, at=c(as.Date("2005-01-01"),as.Date("2007-01-01"),as.Date("2009-01-01"),as.Date("2011-01-01"),as.Date("2013-01-01"),as.Date("2015-01-01")
                               ,as.Date("2017-01-01")), labels=seq(2005,2017,2))

plot(xaxt="n",cex.axis=1.5,cex.lab=1.5,cex.main=2,main="(2) Expenditures",as.Date(agg_time$halfyear),agg_time$amount,type="l",xlab="Half-Years",ylab="Lobbying Expenditures /\nPayments to Lobbyists")
axis(side=1,cex.axis=1.5, at=c(as.Date("2005-01-01"),as.Date("2007-01-01"),as.Date("2009-01-01"),as.Date("2011-01-01"),as.Date("2013-01-01"),as.Date("2015-01-01")
                               ,as.Date("2017-01-01")), labels=seq(2005,2017,2))
